package gov.dhs.cis.soa.readers;

import gov.dhs.cis.soa.SOAData;
import gov.dhs.cis.soa.Utils;
import gov.dhs.cis.soa.data.SecurityComp;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class SecurityCompReader extends ExcelReader {
	static final Logger loger = Logger.getLogger(SecurityCompReader.class);

	private final String SEC_COMP_FILE_NAME = "USCIS Architecture Components - Security Architecture.xls";
	private String SHEET_NAME = "Security Capabilities";
	
	private int NAME_COL_NUM = 0;
	private int IN_COL_NUM = 1;
	private int OUT_COL_NUM = 2;
	private int LAYER_COL_NUM = 3;
	private int REL_COL_NUM = 4;
	private int IMPL_COL_NUM = 5;
	
	private int MAX_ROWS = 20;
		
	public SOAData extractSecComponents(SOAData soaData) {
		loger.debug("Extracting Security Components layer Sheet...");
		HSSFWorkbook w = openFile(SEC_COMP_FILE_NAME);
		HSSFSheet s1 = w.getSheet(SHEET_NAME);
		int rows = s1.getLastRowNum();
		loger.debug("S1 rows =" + rows);
		
		ArrayList<SecurityComp> secCompList = new ArrayList<SecurityComp>();
		soaData.secCompList = secCompList;
		
//		if(!Constants.bResolveSecConfig)
//			return soaData;
		
		for (int n = 2; n < MAX_ROWS; n++) {
			HSSFRow row = s1.getRow(n);
			if(row == null)
				continue;
			String name = getCellStringValue(w, row.getCell(NAME_COL_NUM));;
			if(Utils.isEmpty(name)) {
				loger.warn("Security Component Name is Null/Empty at row : " + n + ", ignoring this row.");
				continue;
			}
			String input = getCellStringValue(w, row.getCell(IN_COL_NUM));
			String output = getCellStringValue(w, row.getCell(OUT_COL_NUM));
			String layer = getCellStringValue(w, row.getCell(LAYER_COL_NUM));
			String rel = getCellStringValue(w, row.getCell(REL_COL_NUM));
			String impl = getCellStringValue(w, row.getCell(IMPL_COL_NUM));
			
			SecurityComp iObj = new SecurityComp();
			iObj.id = Utils.getNextId();
			iObj.name = name;
			iObj.procInput = input;
			iObj.procOutput = output;
			iObj.release = Utils.convert2Release(rel);
			iObj.layer = layer;
			iObj.impl = impl;
			iObj.createDescription();
			
			secCompList.add(iObj);
		}
		closeAll(w);
		return soaData;
	}
}
